package com.wang.tool.serialize;

import java.io.*;
import java.util.Base64;  

/**  
 * Utility class for JDK serialization.  
 */  
public class JdkSerializationUtil {  

    /**  
     * Serializes a Serializable object into a byte array.  
     *   
     * @param obj the object to serialize  
     * @return the serialized byte array  
     * @throws SerializationException if an error occurs during serialization  
     */  
    public static byte[] serialize(Serializable obj) throws SerializationException {  
        if (obj == null) {  
            return new byte[]{};  
        }  
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();  
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {  
            oos.writeObject(obj);  
            return bos.toByteArray();  
        } catch (Throwable e) {
            throw new SerializationException(e, SerializationException.SERIALIZE_ERROR, e.getMessage());  
        }  
    }  

    /**  
     * Deserializes a byte array into a Serializable object.  
     *   
     * @param source the byte array to deserialize  
     * @param <T> the type of the object  
     * @return the deserialized object  
     * @throws SerializationException if an error occurs during deserialization  
     */  
    public static <T extends Serializable> T deserialize(byte[] source) throws SerializationException {
        if (source == null || source.length == 0) {  
            return null;  
        }  
        try (ByteArrayInputStream bis = new ByteArrayInputStream(source);  
             ObjectInputStream ois = new ObjectInputStream(bis)) {  
            return (T) ois.readObject();  
        } catch (Throwable e) {
            throw new SerializationException(e, SerializationException.DESERIALIZE_ERROR, e.getMessage());  
        }  
    }  

    /**  
     * Serializes a Serializable object into a Base64 encoded string.  
     *   
     * @param obj the object to serialize  
     * @return the Base64 encoded string  
     * @throws SerializationException if an error occurs during serialization  
     */  
    public static String serializeToStr(Serializable obj) throws SerializationException {  
        byte[] serializedData = serialize(obj);
        if (serializedData.length==0) {
            return null;
        }
        return Base64.getEncoder().encodeToString(serializedData);  
    }

    /**  
     * Deserializes a Base64 encoded string into a Serializable object.  
     *   
     * @param str the Base64 encoded string to deserialize  
     * @param <T> the type of the object  
     * @return the deserialized object  
     * @throws SerializationException if an error occurs during deserialization  
     */  
    public static <T extends Serializable> T deserializeFromStr(String str) throws SerializationException {  
        if (str == null || str.isEmpty()) {  
            return null;  
        }  
        byte[] data = Base64.getDecoder().decode(str);  
        return deserialize(data);  
    }
}